﻿using Microsoft.IdentityModel.Tokens;
using socialnetworkApp.Domain.IRepository;
using socialnetworkApp.Domain.IRepository.Repository;
using socialnetworkApp.Domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Google.Protobuf.Reflection.SourceCodeInfo.Types;

namespace socialnetworkApp.Domain.Manager
{
    public class JobManager
    {

        private readonly IJobListingRepository _jobListingRepository;
        private readonly IJobUserRepository _jobUserRepository;
        public JobManager(IJobListingRepository jobListingRepository, IJobUserRepository jobUserRepository)
        {
            _jobListingRepository = jobListingRepository;
            _jobUserRepository = jobUserRepository;

        }
        /// <summary>
        /// 登录接口
        /// </summary>
        /// <param name="jobUser"></param>
        /// <returns></returns>
        public async Task<JobUsers> login(JobUsers jobUser)
        {
            string sql = "SELECT * FROM job_users WHERE username = @username AND password = @password;";

            return await _jobUserRepository.SelectFirst(jobUser, sql);
        }
        /// <summary>
        /// 根据邮箱号来判断用户是否存在
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        public async Task<JobUsers> IsExistUser(JobUsers users)
        {

            string sql = "SELECT u.user_id,u.email FROM `job_users` u where  u.email=@email;";
            return await _jobUserRepository.SelectFirst(users, sql);
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        public async Task<long> AddUser(JobUsers users)
        {

            string sql = "INSERT  into  job_users(user_id,role,nickname,email,`password`,username) VALUE(@user_id,@role,@nickname,@email,@password,@username);";
            return await _jobUserRepository.Insert(users, sql);
        }
        public async Task<JobUsers> FindUser(JobUsers jobUsers)
        {
            string sql = "SELECT user_id,nickname,email,phone FROM  job_users WHERE user_id=@user_id;";

            return await _jobUserRepository.SelectFirst(jobUsers, sql);
        }

        /// <summary>
        /// 获取hr身份的人的id
        /// </summary>
        /// <returns></returns>
        public async Task<List<JobUsers>> GetHrUser() {
            string sql = "SELECT  user_id,nickname  from job_users WHERE role=@Role;";
            JobUsers jobUsers = new JobUsers { Role=2};
            return await _jobUserRepository.Select(jobUsers,sql);
        }


        public async Task<List<JobListing>> SearchJobList(JobListing jobListing)
        {

            string sql = "SELECT job_id, u_id, job_title, location, job_description, qualifications, benefits, salary_range,company_Profile ," +
                " safety_Notice, additional_Info , experience_required,education_required,  company_name,   job_listing.created_at FROM " +
                " job_listing WHERE     job_id IS NOT NULL ";
            string Wheresql = "";
            if (!jobListing.Job_Title.IsNullOrEmpty())
            {
                Wheresql += "AND job_title LIKE CONCAT('%',@Job_Title, '%')";
            }
            if (!jobListing.Location.IsNullOrEmpty())
            {
                Wheresql += "AND location LIKE CONCAT('%',@location, '%')";
            }
            //学历筛选
            if (!jobListing.Education_Required.IsNullOrEmpty())
            {

                Wheresql += " AND education_required  LIKE CONCAT('%',@Education_Required, '%')";


            }
            //薪资筛选
            if (!jobListing.Salary_Range.IsNullOrEmpty())
            {
                string salaryRange = "15,000-20,000";
                string[] parts = jobListing.Salary_Range.Split('-');

                string lowerValue = parts[0].Replace(",", ""); // 处理前面的值
                string upperValue = parts[1].Replace(",", ""); // 处理后面的值
                Wheresql += " AND  CAST(REPLACE(SUBSTRING_INDEX(salary_range, '-', 1), ',', '') AS UNSIGNED) >= "+ lowerValue + " " +
                    "AND CAST(REPLACE(SUBSTRING_INDEX(salary_range, '-', -1), ',', '') AS UNSIGNED) <= "+ upperValue + " ";
            }
            if (!jobListing.Company_Name.IsNullOrEmpty())
            {
                Wheresql += "AND company_name LIKE CONCAT('%',@Company_Name, '%')";
            }

            Wheresql += "  ORDER BY   created_at DESC;";
            sql += Wheresql;
            return await _jobListingRepository.Select(jobListing, sql);
        }
    }
}
